<?php

class Cliente {

  public static function getFromId($id, $db = null) {
    $wasNull = false;
    if($db === null) {
      $wasNull = true;
      $db = new DBUtils();
      $db->connect();
    }
    $db->runQuery("select * from DadosCliente where idCliente = ?", array($id));
    $item = $db->getItem();
    if($wasNull) {
      $db->close();
    }
    return $item;
  }
  
  public static function getFromEmail($email, $db = null) {
    $wasNull = false;
    if($db === null) {
      $wasNull = true;
      $db = new DBUtils();
      $db->connect();
    }
    $db->runQuery("select * from DadosCliente where email = ?", array($email));
    $item = $db->getItem();
    if($wasNull) {
      $db->close();
    }
    return $item;
  }
  
  public static function ownsReservation($idCliente, $idReserva, $db = null) {
    $wasNull = false;
    if($db === null) {
      $wasNull = true;
      $db = new DBUtils();
      $db->connect();
    }
    $db->runQuery("select idReserva from ReservasUsuario(?) where idReserva = ?",
                  array($idCliente, $idReserva));
    $item = $db->getItem();
    if($wasNull) {
      $db->close();
    }
    return $item !== null;
  }
}
